from django import forms
from app01 import models
from django.core.validators import ValidationError


class ProjectNew(forms.Form):
    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request

    def clean_project_name(self):
        user_id = self.request.session.get('user_id')
        project_name = self.cleaned_data.get('project_name')
        project_text = self.cleaned_data.get('project_text')
        print(user_id, project_name, project_text)
        if not project_text:
            raise ValidationError('项目名不能为空！')
        project_ob = models.ProjectInformation.objects.filter(created_by=user_id, project_name=project_name).first()
        if project_ob:
            raise ValidationError('项目名已存在！')
        models.ProjectInformation.objects.create(project_name=project_name, describe=project_text, color='#2064DE',
                                                 star_labe='False', project_in_member_qty=1, created_by=user_id,
                                                 used_space=0)
        return user_id
